package cn.hhit.ximalaya.fragments;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.GussLikeAlbumList;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hhit.ximalaya.R;
import cn.hhit.ximalaya.adapters.AlbumListAdapter;
import cn.hhit.ximalaya.base.BaseFragment;
import cn.hhit.ximalaya.utils.Constants;
import cn.hhit.ximalaya.utils.LogUtil;


public class RecommendFragment extends BaseFragment {
    public static  String TAG="RecommendFragment ";


    private RecyclerView recommend_recyclerview;
    private AlbumListAdapter albumListAdapter;

    public RecommendFragment() {
        // Required empty public constructor
    }


    @Override
    protected View onSubViewLoaded(LayoutInflater layoutInflater, ViewGroup container) {
        View rootView = layoutInflater.inflate(R.layout.fragment_recommend, container, false);


        //处理recyclerView
        recommend_recyclerview = rootView.findViewById(R.id.recommend_recyclerview);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this.getContext());
        recommend_recyclerview.setLayoutManager(linearLayoutManager);
        //创造适配器 RecyclerView.Adapter<InnerHolder>
        albumListAdapter = new AlbumListAdapter();
        //获取外部数据
        getRecommendData();

       // albumListAdapter.setData();
        recommend_recyclerview.setAdapter(albumListAdapter);

        recommend_recyclerview.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

                outRect.top = UIUtil.dip2px(view.getContext(), 5);
                outRect.bottom = UIUtil.dip2px(view.getContext(), 5);
                outRect.left = UIUtil.dip2px(view.getContext(), 5);
                outRect.right = UIUtil.dip2px(view.getContext(), 5);
            }


        });


        return rootView;
    }
    //配置recyclerView



    /*
     * 功能：获取推荐数据，就是“获取猜你喜欢专辑”
     * */
    public  void   getRecommendData(){
        Map<String, String> map = new HashMap<String, String>();
        map.put(DTransferConstants.LIKE_COUNT, Constants.RECOMMEND_DATA +"");
        CommonRequest.getGuessLikeAlbum(map, new IDataCallBack<GussLikeAlbumList>() {
            @Override
            public void onSuccess(GussLikeAlbumList gussLikeAlbumList) {
                if(gussLikeAlbumList!=null){
                    List<Album>   albumList = gussLikeAlbumList.getAlbumList();
                    LogUtil.d(TAG,"本次获得的推荐数据数量为"+albumList.size());
                    setRecyclerViewAapterData(albumList);




                }

            }
            @Override
            public void onError(int i, String s) {
                LogUtil.e(TAG,"error--->"+i);

            }
        });


    }
   /*
   * 功能：给adapter传递数据
   *
   * */
    private void setRecyclerViewAapterData(List<Album> albumList) {
        albumListAdapter.setData(albumList);




    }

}